﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CCWin;
using SuperMap.Mapping;

namespace MDF
{
    /// <summary>
    /// 坐标定位
    /// </summary>
    public partial class ZBDW : CCSkinMain
    {
        //初始变量
        //private Map m_map;
        private Map[] m_maps;
        /// <summary>
        /// 构造函数
        /// </summary>
        public ZBDW(Map map)
        {
            InitializeComponent();
            //获取数据
            //m_map = map;
            //填充数据
            MapCenter_ToText();

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="maps">所有控件的地图集合</param>
        public ZBDW(Map[] maps)
        {
            InitializeComponent();
            //获取数据
            m_maps = maps;
            //填充数据
            MapCenter_ToText();

        }
        /// <summary>
        /// 获取单位并填充默认数据
        /// </summary>
        public void MapCenter_ToText()
        {
            switch (m_maps[0].CoordUnit)//判断单位（部分）
            {
                //case SuperMap.Data.Unit.Millimeter:
                //    break;
                //case SuperMap.Data.Unit.Centimeter:
                //    break;
                //case SuperMap.Data.Unit.Decimeter:
                //    break;
                case SuperMap.Data.Unit.Meter:
                    this.X_Unit.Text = "m";
                    this.Y_Unit.Text = "m";
                    DuFenMiao_Check.Enabled = false;
                    break;
                case SuperMap.Data.Unit.Kilometer:
                    this.X_Unit.Text = "km";
                    this.Y_Unit.Text = "km";
                    DuFenMiao_Check.Enabled = false;
                    break;
                //case SuperMap.Data.Unit.Inch:
                //    break;
                //case SuperMap.Data.Unit.Foot:
                //    break;
                //case SuperMap.Data.Unit.Yard:
                //    break;
                //case SuperMap.Data.Unit.Mile:
                //    break;
                //case SuperMap.Data.Unit.Second:
                //    break;
                //case SuperMap.Data.Unit.Minute:
                //    break;
                case SuperMap.Data.Unit.Degree:
                    DuFenMiao_Check.Enabled = true;//启用度分秒的转换
                    break;
                //case SuperMap.Data.Unit.Radian:
                //    break;
                default:
                    DuFenMiao_Check.Enabled = false;
                    break;
            }
            //填充地图中心点的数据
            this.X_Text.Text = m_maps[0].Center.X.ToString();
            this.Y_Text.Text = m_maps[0].Center.Y.ToString();
        }
        /// <summary>
        /// 确定执行定位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Button_Click(object sender, EventArgs e)
        {
            try
            {
                double x = 0, y = 0;
                if (DuFenMiao_Check.Checked)//当显示度分秒时
                {
                    if(this.X_D_Text.Text == "-") { this.X_D_Text.Text = "0"; }
                    if(this.Y_D_Text.Text == "-") { this.Y_D_Text.Text = "0"; }
                    x = double.Parse(this.X_D_Text.Text) + double.Parse(this.X_M_Text.Text) / 60 + double.Parse(this.X_S_Text.Text) / 3600;
                    y = double.Parse(this.Y_D_Text.Text) + double.Parse(this.Y_M_Text.Text) / 60 + double.Parse(this.Y_S_Text.Text) / 3600;
                }
                else
                {
                    if (this.X_Text.Text == "-") { this.X_Text.Text = "0"; }
                    if (this.Y_Text.Text == "-") { this.Y_Text.Text = "0"; }
                    x = double.Parse(this.X_Text.Text);
                    y = double.Parse(this.Y_Text.Text);
                }
                foreach (Map map in m_maps)
                {
                    //double scale = map.Scale;//获取map当前比例尺
                    map.Center = new SuperMap.Data.Point2D(x, y);//设置map中心点
                    map.Refresh();//刷新map
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 切换度分秒的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DuFenMiao_Check_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (DuFenMiao_Check.Checked)//为true显示度分秒
                {
                    XY_Panel.Enabled = false;
                    XY_Panel.Visible = false;
                    DMS_panel.Enabled = true;
                    DMS_panel.Visible = true;
                    DMS_panel.Location = XY_Panel.Location;
                    if (X_Text.Text != "-")
                    {
                        double X = double.Parse(X_Text.Text);
                        X_D_Text.Text = ((int)X).ToString();
                        X_M_Text.Text = ((int)((X - int.Parse(X_D_Text.Text)) * 60)).ToString();
                        X_S_Text.Text = ((int)(((X - int.Parse(X_D_Text.Text)) * 60 - int.Parse(X_M_Text.Text)) *60 + 0.4)).ToString();
                    }
                    if (Y_Text.Text != "-")
                    {
                        double Y = double.Parse(Y_Text.Text);
                        Y_D_Text.Text = ((int)Y).ToString();
                        Y_M_Text.Text = ((int)((Y - int.Parse(Y_D_Text.Text)) * 60)).ToString();
                        Y_S_Text.Text = ((int)(((Y - int.Parse(Y_D_Text.Text)) * 60 - int.Parse(Y_M_Text.Text)) * 60 + 0.4)).ToString();
                    }
                }
                else
                {
                    DMS_panel.Visible = false;
                    DMS_panel.Visible = false;
                    XY_Panel.Enabled = true;
                    XY_Panel.Visible = true;
                    this.X_Text.Text = (double.Parse(this.X_D_Text.Text) + double.Parse(this.X_M_Text.Text) / 60 + double.Parse(this.X_S_Text.Text) / 3600).ToString();
                    this.Y_Text.Text = (double.Parse(this.Y_D_Text.Text) + double.Parse(this.Y_M_Text.Text) / 60 + double.Parse(this.Y_S_Text.Text) / 3600).ToString();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 只允许输入数字、一个小数点、一个-号、删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == (char)8 || e.KeyChar == '-'))
                {
                    e.Handled = true;
                }
                else
                {
                    string text = ((TextBox)sender).Text;
                    if (e.KeyChar >= '0' && e.KeyChar <= '9' && text == "0")
                    {
                        e.Handled = true;
                        ((TextBox)sender).Text = (e.KeyChar - 48).ToString();
                    }
                    if (e.KeyChar == '.')
                    {
                        int num = text.IndexOf('.');
                        if (text.Length == 0 || num != -1)
                        {
                            e.Handled = true;
                        }
                    }
                    if (e.KeyChar == '-')
                    {
                        e.Handled = true;
                        if (text == "0")
                        {
                            ((TextBox)sender).Text = "-";
                        }
                    }
                    if (e.KeyChar == (char)8 && text.Length == 1)
                    {
                        e.Handled = true;
                        ((TextBox)sender).Text = "0";
                    }
                }
                ((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 只允许输入数字，经度范围[-180,180]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void X_D_Text_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8 || e.KeyChar == '-'))
                {
                    e.Handled = true;
                }
                else
                {
                    string text = X_D_Text.Text;
                    if (e.KeyChar >= '0' && e.KeyChar <= '9')
                    {
                        if (text == "0")
                        {
                            e.Handled = true;
                            X_D_Text.Text = (e.KeyChar - 48).ToString();
                        }
                        else if(text != "-")
                        {
                            int num = 0;
                            if (int.Parse(text) > 0)
                            {
                                num = int.Parse(text) * 10 + e.KeyChar - 48;
                            }
                            else
                            {
                                num = int.Parse(text) * 10 - e.KeyChar + 48;
                            }
                            if (num < -180 || num > 180)
                            {
                                e.Handled = true;
                                //MessageBox.Show("经度输入有误，经度范围[-180,180]", "警告：");
                            }
                        }
                    }
                    if (e.KeyChar == '-')
                    {
                        e.Handled = true;
                        if (text == "0")
                        {
                            X_D_Text.Text = "-";
                        }
                    }
                    if (e.KeyChar == (char)8 && text.Length == 1)
                    {
                        e.Handled = true;
                        X_D_Text.Text = "0";
                    }
                }
                X_D_Text.SelectionStart = X_D_Text.Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 只允许输入数字，纬度范围[-90,90]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Y_D_Text_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8 || e.KeyChar == '-'))
                {
                    e.Handled = true;
                }
                else
                {
                    string text = Y_D_Text.Text;
                    if (e.KeyChar >= '0' && e.KeyChar <= '9')
                    {
                        if (text == "0")
                        {
                            e.Handled = true;
                            Y_D_Text.Text = (e.KeyChar - 48).ToString();
                        }
                        else if (text != "-")
                        {
                            int num = 0;
                            if (int.Parse(text) > 0)
                            {
                                num = int.Parse(text) * 10 + e.KeyChar - 48;
                            }
                            else
                            {
                                num = int.Parse(text) * 10 - e.KeyChar + 48;
                            }
                            if (num < -90 || num > 90)
                            {
                                e.Handled = true;
                                //MessageBox.Show("纬度输入有误，经度范围[-90,90]", "警告：");
                            }
                        }
                    }
                    if (e.KeyChar == '-')
                    {
                        e.Handled = true;
                        if (text == "0")
                        {
                            Y_D_Text.Text = "-";
                        }
                    }
                    if (e.KeyChar == (char)8 && text.Length == 1)
                    {
                        e.Handled = true;
                        Y_D_Text.Text = "0";
                    }
                }
                Y_D_Text.SelectionStart = Y_D_Text.Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 只允许输入数字，范围[0,60]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MS_Text_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox m_textBox = (TextBox)sender;
                string text = m_textBox.Text;
                if (text == "" && !(e.KeyChar >= '0' && e.KeyChar <= '6'))
                {
                    e.Handled = true;
                }
                else if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == (char)8))
                {
                    e.Handled = true;
                }
                else
                {
                    if (e.KeyChar >= '0' && e.KeyChar <= '9')
                    {
                        if (text == "" || text == "0")
                        {
                            e.Handled = true;
                            m_textBox.Text = (e.KeyChar - 48).ToString();
                        }
                        else
                        {
                            text += (e.KeyChar - 48);
                            double num = double.Parse(text);
                            if (num < 0 || num > 60)
                            {
                                e.Handled = true;

                            }
                        }
                    }
                    if (e.KeyChar == '.')
                    {
                        int num = text.IndexOf('.');
                        if (text.Length == 0 || num != -1)
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == (char)8 && text.Length == 1)
                        {
                            e.Handled = true;
                            m_textBox.Text = "0";
                        }
                    }
                }
                m_textBox.SelectionStart = m_textBox.Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
